package dycloud.GameName.User.controller;


import com.alibaba.fastjson.JSON;
import dycloud.demo.model.JsonResponse;
import dycloud.GameName.User.model.Json_UserDataInfo;
import dycloud.GameName.User.model.Json_UserInfo;
import dycloud.GameName.User.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


//所有用户相关操作
@RestController
@Slf4j
public class UserInfoCtrl {

    //@RequestBody GameRoleRank gameRoleRank   --请求体  Post
    //@RequestParam String sec_openid  --get  请求参数
    //请求头 自带的 参数有
    /*
    *  @PostMapping(path = "/start_game")
       public JsonResponse callContainerExample(HttpServletRequest httpRequest) {
        // 开发者可以直接通过请求头获取直播间信息,无需自行通过token置换

        // 应用id
        String appID = httpRequest.getHeader("X-TT-AppID");
        // 直播间id
        String roomID = httpRequest.getHeader("X-Room-ID");
        // 主播id
        String anchorOpenID = httpRequest.getHeader("X-Anchor-OpenID");
        // 主播头像url
        String avatarUrl = httpRequest.getHeader("X-Avatar-Url");
        // 主播昵称
        String nickName = httpRequest.getHeader("X-Nick-Name");
    * */

    /**
     * 获取 单个玩家 信息
     */
    @Autowired
    private UserInfoService userInfoService;
    @PostMapping(path = "/getUserInfo")
    public JsonResponse getUserInfo(@RequestBody Json_UserInfo userInfo) {
        log.info("UserPid --> {} <--", userInfo.playid);
        JsonResponse response = new JsonResponse();
        try {
            Json_UserInfo us = userInfoService.checkAndInsertUserInfo(userInfo);
            if (us ==null)
            {
                response.failure("MongoDB 数据库 查询玩家信息  连接异常");
                return response;
            }
            //有该玩家  查询并插入胜利点数据
            Json_UserDataInfo jd = userInfoService.getUserDataInfo(us.getPlayid());
            if (jd ==null)
            {
                response.failure("MongoDB 数据库 查询玩家数据 连接异常");
                return response;
            }
            //传输到客户端
            response.success( JSON.toJSONString(jd));
            return response;
        }catch (Exception e) {
            response.failure("MongoDB 数据库 查询 连接异常");
            return response;
        }

    }

    @GetMapping(path = "/getWinCount")
    public JsonResponse getUserWinCount(@RequestParam String playid) {
        JsonResponse response = new JsonResponse();
        try {
            long winCount = userInfoService.getUserWinCount(playid);
            //传输到客户端
            response.success( JSON.toJSONString(winCount));
            return response;
        }catch (Exception e){
            response.failure("获取胜利点 失败");
            return response;
        }

    }
    @PostMapping(path = "/setWinCount/{playid}")
    public JsonResponse setUserWinCount(@PathVariable String playid,@RequestParam long winCount) {
        JsonResponse response = new JsonResponse();
        try {
            boolean isSuccess = userInfoService.setUserWinCount(playid,winCount);
            //传输到客户端
            response.success( JSON.toJSONString(isSuccess));
            return response;
        }catch (Exception e){
            response.failure("设置 失败");
            return response;
        }

    }


    /**
     * 测试抖音 直播 启动 参数
     */
    @GetMapping(path = "/testHttp")
    public JsonResponse finishGameExample(HttpServletRequest httpRequest ,@RequestParam String pid) {

        log.info("请求参数  {}", pid);
        log.info("请求的游戏ID --> {}",httpRequest.getHeader("X-TT-AppID"));
        JsonResponse response = new JsonResponse();
        String sss = "抖音直播 启动看看 有没有 appID -->"+httpRequest.getHeader("X-TT-AppID");
        response.success(sss);
        return response;
    }
}
